<?php
namespace app\tool;
use think\facade\Db;
/**
 *
 */
class Utils
{

    public static function multi_to_one($multi_arr=[],$k='children',$f=1)
    {
        static $result_arr;
        if($f==1) $result_arr=[];
        foreach ($multi_arr as $key => $val) {
            if (isset($val[$k]) and is_array($val[$k])) {
               self::multi_to_one($val[$k],$key,2);
            }
            unset($val[$k]);
            $result_arr[] = $val;

        }
        return $result_arr;
    }

    /**
     * @param $token
     * @return array|false|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getUser($token=''){
        $user_token=Db::name('user_token')->where('loginToken',$token)->find();
        if(!$user_token){
            return false;
        }
        $user=Db::name('user')->where('id',$user_token['userId'])->find();
        return $user;
    }

    /**
     * 生成数 结构
     * @param $array
     * @param $fu
     * @param $pid
     * @param $level
     * @return array
     */
    public static function tree($array, $pid=0,$fu='parent_id',$level=1){
        $tree = [];
        static $tree_id;
        if($level==1){$tree_id=[];}
        foreach ($array as $k=>$v){
            if(in_array($v['id'],$tree_id)){
                unset($array[$k]);
            }
        }
        foreach ($array as $key => $value) {
            if ($value[$fu] == $pid) {
                $tree_id[]=$value['id'];
                $value['children'] = self::tree($array, $value['id'],$fu,$level+1);
                $value['level']=$level;
                $tree[] = $value;
            }
        }
        unset($tree_id);
        return $tree;
    }

    public static function tree2($array , $fu='parent_id',$level=0){
        //第一步 构造数据
        $items = array();
        foreach($array as $value){
            $items[$value['id']] = $value;
        }
        //第二部 遍历数据 生成树状结构
        $tree = array();
        foreach($items as $key => $item){
            if(isset($items[$item[$fu]])){
                $items[$item[$fu]]['children'][] = &$items[$key];
            }else{
                $tree[] = &$items[$key];
            }
        }
        return $tree;
    }
    /**
     * 为每一层添加级别
     * @param $res
     * @param $fid
     * @param $level
     * @return array
     */
    public static function sort($res, $fname='parent_id',$fid=0, $level=0){
        //这样需要学习一下
        static $arr;
        if($level==0){ $arr=[];}
        foreach ($res as $key => $value) {
            if($value[$fname]==$fid){
                $value['level']=$level;
                $arr[]=$value;
                self::sort($res,$fname,$value['id'],$level+1);
            }
        }
        return $arr;
    }


    /**
     * 在数组内找到所有孩子
     * @param $id
     * @param $arr
     * @param $level
     * @return array
     */
    public static function arrFindall($id=[], $arr='',$fname='parent_id',$level=1){
        static $narr;
        if($level==1){ $narr=[];}
        $ids=[];
        foreach ($arr as  $k=>$v) {
            if (in_array($v[$fname], $id)) {
                $v['level']=$level;
                $narr[] = $v;
                $ids[] = $v['id'];
                unset($arr[$k]);
            }
        }
        if(sizeof($ids)){
            self::arrFindall($ids, $arr, $fname, $level + 1);
        }
        return $narr;
    }






    /**
     * 找到所有孩子
     * @param $id
     * @param $name
     * @param $level
     * @return array
     */
    public static function findall($id=[], $name='',$field='*', $isId=false,$fname='parent_id',$level=1,$order='id desc'){
        static $arr;
        if($level==1){ $arr=[];}
        $ch=Db::name($name)->where($fname,'in',$id)->where('delTime',0)->column('id');
        if($level==1){ $ch=array_merge($id,$ch);}
        $arr=array_merge($arr,$ch);
        if(sizeof($ch)){
            return self::findall($ch,$name,$field, $isId,$fname,$level+1,$order);
        }
        if(!$isId){
            $arr=Db::name($name)->where('id','in',$arr)->where('delTime',0)->field($field)->order($order)->select()->toArray();
        }
        return $arr;
    }
    /**
     * 找到所有父亲
     * @param $id
     * @param $name
     * @param $level
     * @return array
     */
    public static function findfu($id=[], $name='',$field='*', $isId=false,$fname='parent_id', $level=1,$order='id desc'){
        static $arr;
        static $linid=[];
        if($level==1){ $arr=[];$linid=$id;}

        $ch=Db::name($name)->where('id','in',$id)->where('delTime',0)->column($fname);
        $arr=array_merge($arr,$ch);
        if(sizeof($ch)){
           return self::findfu($ch,$name,$field, $isId,$fname,$level+1,$order);
        }
        $arr=array_merge($arr,$linid);
        if(!$isId){
            $arr=Db::name($name)->where('id','in',$arr)->where('delTime',0)->field($field)->order($order)->select()->toArray();
        }
        return $arr;
    }
    public static function findfu2id($id=[], $name='', $fname='parent_id', $level=1,$top=0){
        static $arr;
        static $linid=[];
        static $topid=0;
        if($level==1){ $arr=[];$linid=$id;$topid=$top;}
        $ch=Db::name($name)->where('id','in',$id)->where('delTime',0)->column($fname);
        $arr=array_merge($arr,$ch);
        if(sizeof($ch)){
            self::findfu($ch,$name,$fname,$level+1);
        }
        $arr=array_merge($arr,$linid);
        foreach ($arr as $arrk=>$arrv){
            if($arrv < $topid){
                unset($arr[$arrk]);
            }
        }
        return $arr;
    }
}